<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/15 0015
 * Time: 10:59
 */

namespace Cohuna\Services;


use Cohuna\Models\CohunaOrder;
use Cohuna\Models\CohunaPay;

class CohunaPayService extends ServiceBase
{
    /**
     * 更新支付配置
     * @param array $pay_config
     * @return bool
     * @throws \Exception
     */
    public function update(array $pay_config)
    {
        if($this->check($pay_config)){

            return true;
        }

       return false;
    }

    /**
     * 初始化支付配置
     * @param array $pay_config
     * @return array
     * @throws \Exception
     */
    protected function _initCohunaPayData(array $pay_config)
    {
        $data = array();

        $data['pay_status'] = $pay_config['pay_status'] == 'y' ? 'y' : 'n';
        $data['pay_name']   = $pay_config['pay_name'];
        $data['sign_type']  = $pay_config['encryption'];
        $data['gateway']    = $pay_config['payment_gateway'];
        $data['remark']     = $pay_config['remark'];
        $data['pay_platform'] = $pay_config['payment_platform'];



        if(empty($pay_config['payment_config_id']) === false){
            $data['pay_id'] = $pay_config['payment_config_id'];
        }
        if($pay_config['payment_platform'] == 'wxpay' || $pay_config['payment_platform'] == 'wxpay_global'){
            if(!isset($pay_config['mch_id']) || empty($pay_config['mch_id'])){
                throw new \Exception('微信商户号不能为空');
            }
            $data['pay_account'] = $pay_config['mch_id'];


            $data['pay_config'] = array();
            $data['pay_config']['app_id'] = $pay_config['app_id'];
            $data['pay_config']['mch_id'] = $pay_config['mch_id'];
            $data['pay_config']['encryption_key'] = $pay_config['encryption_key'];
            $data['pay_config']['rootca'] = $pay_config['rootca'];
            $data['pay_config']['merchant_cert'] = $pay_config['merchant_cert'];
            $data['pay_config']['merchant_cert_key'] = $pay_config['merchant_cert_key'];
        }elseif ($pay_config['payment_platform'] == 'alipay' ){

            $data['pay_account'] = $pay_config['alipay_account'];

            $data['pay_config'] = array();

            if($pay_config['payment_gateway'] == 'wireless' || $pay_config['payment_gateway'] == '')
            $data['pay_config']['partner'] = $pay_config['partner'];
            if($pay_config['encryption'] == 'md5'){
                $data['pay_config']['encryption_key'] = $pay_config['encryption_key'];
            }elseif ($pay_config['encryption'] == 'rsa1'){
                $data['pay_config']['alipay_private_cert'] = $pay_config['alipay_private_cert'];
                $data['pay_config']['alipay_public_cert'] = $pay_config['alipay_public_cert'];
            }elseif ($pay_config['encryption'] == 'dsa'){
                $data['pay_config']['alipay_private_cert'] = $pay_config['alipay_private_cert'];
                $data['pay_config']['alipay_public_cert'] = $pay_config['alipay_public_cert'];
            }

        }


        return $data;
    }

    /***
     * 校验支付配置数据
     * @param array $pay_config
     * @return bool
     * @throws \Exception
     */
    public function check(array  $pay_config)
    {
        if(!isset($pay_config['payment_platform']) || !isset($this->config->payment_platform_list[$pay_config['payment_platform']])){
            throw new \Exception('支付平台错误');
        }
        if(!isset($pay_config['payment_gateway']) || !isset($this->config->payment_platform_list[$pay_config['payment_platform']]['gateway_list'][$pay_config['payment_gateway']])){
            throw new \Exception('支付网关错误');
        }
        if(!isset($pay_config['encryption']) || !isset($this->config->payment_platform_list[$pay_config['payment_platform']]['gateway_list'][$pay_config['payment_gateway']]['encryption_list'][$pay_config['encryption']])){
            throw new \Exception('加密方式错误');
        }
        if(empty($pay_config['pay_name'])){
            throw new \Exception('支付名称不能为空');
        }
        if(empty($pay_config['payment_config_id']) === false){
            $count = CohunaPay::count(array(
                'pay_id = :pay_id:',
                'bind' => array('pay_id'=>$pay_config['payment_config_id'])
            ));
            if($count <= 0){
                throw new \Exception('配置信息不存在');
            }
        }
        return true;
    }

    /**
     * 删除指定的支付配置
     * @param string $pay_id
     * @param null|string $mch_id
     * @return bool
     */
    public function delete($pay_id,$mch_id = null)
    {

        $conditions = array('pay_id = :pay_id:','bind' => array('pay_id' => $pay_id));
        if(empty($mch_id) === false){
            $conditions[0] .= ' AND merchant_id = :merchant_id:';
            $conditions['bind']['merchant_id'] = $mch_id;
        }

        $cohuna_pay = CohunaPay::findFirst($conditions);
        if(empty($cohuna_pay)){
            return false;
        }

        //如果该支付配置没有下过订单，则直接删除
        if(CohunaOrder::count(array('pay_id=:pay_id:','bind'=>array('pay_id'=>$pay_id))) <= 0){
            return $cohuna_pay->delete();

        }else{
            $cohuna_pay->is_delete = 1;
            return $cohuna_pay->save();
        }
    }
}